package com.cloud.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cloud.widget.utils.KeyBoardUtils;

/**
 * @Author： Chen
 * @Date： 2020/4/23
 * @Desc：
 */
public class ScanText extends FrameLayout {
    private Context context;
    private FrameLayout flCameraScan;
    private EditText etInputCode;
    private FrameLayout flClear;
    private TextView tvPreviousScan;
    private CheckBox cbUniqueCode;
    private OnScanListener scanListener;

    public ScanText(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public ScanText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
        setAttributes(attrs);
    }

    public ScanText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
        setAttributes(attrs);
    }

    private void init() {
        View mView = LayoutInflater.from(context).inflate(R.layout.layout_scan_input, this);
        flCameraScan = ((FrameLayout) mView.findViewById(R.id.fl_camera_scan));
        etInputCode = ((EditText) mView.findViewById(R.id.et_input_code));
        flClear = ((FrameLayout) mView.findViewById(R.id.fl_clear));
        tvPreviousScan = ((TextView) mView.findViewById(R.id.tv_previous_scan));
        cbUniqueCode = ((CheckBox) mView.findViewById(R.id.cb_unique_code));

        etInputCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && s.length() > 0) {
                    flClear.setVisibility(VISIBLE);
                } else {
                    flClear.setVisibility(INVISIBLE);
                }
            }
        });

        flClear.setOnClickListener(v -> {
            if (flCameraScan.getVisibility() == VISIBLE) {
                etInputCode.setText("");
            }
        });

        flCameraScan.setOnClickListener(v -> {
            if (null != scanListener) scanListener.onScan();
        });
    }

    private void setAttributes(AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScanText);
        String hint = typedArray.getString(R.styleable.ScanText_text_hint);
        etInputCode.setHint(hint);

        int max = typedArray.getInt(R.styleable.ScanText_max_length, 64);
        //设置可以输入任意字符
        etInputCode.setInputType(InputType.TYPE_CLASS_TEXT);
        //手动设置maxLength
        InputFilter[] filters = {new InputFilter.LengthFilter(max)};
        etInputCode.setFilters(filters);
    }

    public void addHistory(String scanContent) {
        if (TextUtils.isEmpty(scanContent)) {
            return;
        }
        if (etInputCode != null) {
            etInputCode.setText("");
            tvPreviousScan.setText(scanContent);
            KeyBoardUtils.hideKeyBoard(etInputCode, context);
        }
    }

    public String getText() {
        if (etInputCode != null) {
            Editable editable = etInputCode.getText();
            return editable == null ? "" : editable.toString().trim();
        }
        return "";
    }

    public void setText(String text) {
        if (etInputCode != null) {
            etInputCode.setText(text);
            etInputCode.setSelection(text.length());
        }
    }

    public void setMaxLength(int maxLength) {
        if (etInputCode != null) {
            InputFilter[] filters = {new InputFilter.LengthFilter(maxLength)};
            etInputCode.setFilters(filters);
        }
    }

    public void setHint(String text) {
        if (etInputCode != null) {
            etInputCode.setHint(text);
        }
    }

    public void isShowScan(boolean isShow) {
        if (flCameraScan != null) {
            flCameraScan.setVisibility(isShow ? VISIBLE : GONE);
        }
    }

    public void getFocus() {
        if (etInputCode != null) {
            etInputCode.setFocusable(true);
            etInputCode.setFocusableInTouchMode(true);
            etInputCode.requestFocus();
        }
    }

    public void clearFocus() {
        if (etInputCode != null) {
            etInputCode.clearFocus();
        }
    }

    public boolean isGetFocus() {
        if (etInputCode != null) {
            return etInputCode.hasFocus();
        }
        return false;
    }

    public boolean isUseUnique() {
        if (cbUniqueCode != null && cbUniqueCode.getVisibility() == VISIBLE) {
            return cbUniqueCode.isChecked();
        }
        return false;
    }

    public void setChecked(boolean isCheck) {
        if (cbUniqueCode != null && cbUniqueCode.getVisibility() == VISIBLE) {
            cbUniqueCode.setChecked(isCheck);
        }
    }

    public void isShowUnique(boolean isShow) {
        if (cbUniqueCode != null) {
            cbUniqueCode.setVisibility(isShow ? VISIBLE : GONE);
        }
    }

    public void showUnique() {
        if (cbUniqueCode != null) {
            cbUniqueCode.setVisibility(View.VISIBLE);
        }
    }

    public void hideUnique() {
        if (cbUniqueCode != null) {
            cbUniqueCode.setVisibility(View.GONE);
        }
    }

    public void isClick(boolean isClick) {
        if (etInputCode != null) {
            etInputCode.setFocusable(isClick);
            etInputCode.setFocusableInTouchMode(isClick);
        }
        if (flCameraScan != null) {
            flCameraScan.setClickable(isClick);
        }
    }

    public void setSelection(int length) {
        etInputCode.setSelection(length);
    }

    @Override
    public void setOnKeyListener(OnKeyListener l) {
        if (etInputCode != null) {
            etInputCode.setOnKeyListener(l);
        }
    }

    public void setOnScanListener(ScanText.OnScanListener scanListener) {
        this.scanListener = scanListener;
    }

    public interface OnScanListener {
        void onScan();
    }

}
